package com.example.mydy;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import com.example.mydy.adapter.IncomeRecordAdapter;
import com.example.mydy.model.IncomeRecord;
import com.example.mydy.model.UserInfo;
import com.example.mydy.utils.MockDataManager;
import com.example.mydy.utils.SharedPreferencesManager;

import java.util.ArrayList;
import java.util.List;

public class UserCenterActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private ImageView ivAvatar;
    private TextView tvUsername;
    private TextView tvLevel;
    private TextView tvBalance;
    private TextView tvTodayIncome;
    private TextView tvWeekIncome;
    private TextView tvMonthIncome;
    private RecyclerView rvIncomeRecords;
    private IncomeRecordAdapter incomeRecordAdapter;
    private LinearLayout llWithdraw;
    private LinearLayout llInvite;
    private LinearLayout llTasks;
    private LinearLayout llSettings;
    private String currentUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_center);

        currentUsername = getIntent().getStringExtra("username");
        if (currentUsername == null) {
            currentUsername = "test_user";
        }

        initViews();
        setupToolbar();
        setupClickListeners();
        setupBottomNavigation();
        loadUserData();
        loadIncomeRecords(currentUsername);
    }

    private void initViews() {
        toolbar = findViewById(R.id.toolbar);
        ivAvatar = findViewById(R.id.iv_avatar);
        tvUsername = findViewById(R.id.tv_username);
        tvLevel = findViewById(R.id.tv_level);
        tvBalance = findViewById(R.id.tv_balance);
        tvTodayIncome = findViewById(R.id.tv_today_income);
        tvWeekIncome = findViewById(R.id.tv_week_income);
        tvMonthIncome = findViewById(R.id.tv_month_income);
        rvIncomeRecords = findViewById(R.id.rv_income_records);
        llWithdraw = findViewById(R.id.ll_withdraw);
        llInvite = findViewById(R.id.ll_invite);
        llTasks = findViewById(R.id.ll_tasks);
        llSettings = findViewById(R.id.ll_settings);

        // 设置RecyclerView
        rvIncomeRecords.setLayoutManager(new LinearLayoutManager(this));
        incomeRecordAdapter = new IncomeRecordAdapter();
        rvIncomeRecords.setAdapter(incomeRecordAdapter);
    }

    private void setupToolbar() {
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            getSupportActionBar().setDisplayShowTitleEnabled(true);
        }
    }

    private void setupClickListeners() {
        llWithdraw.setOnClickListener(v -> {
            // TODO: 实现提现功能
        });

        llInvite.setOnClickListener(v -> {
            // TODO: 实现邀请功能
        });

        llTasks.setOnClickListener(v -> {
            // TODO: 实现任务管理功能
        });

        llSettings.setOnClickListener(v -> {
            // TODO: 实现设置功能
        });
    }

    private void setupBottomNavigation() {
        // 获取底部导航视图
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        
        // 设置当前选中项为"我的"
        bottomNavigationView.setSelectedItemId(R.id.nav_my);
        
        // 设置导航菜单项选择监听器
        bottomNavigationView.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.nav_home) {
                // 先设置选中项为首页，然后再跳转
                bottomNavigationView.setSelectedItemId(R.id.nav_home);
                startActivity(new Intent(this, MainActivity.class));
                finish();
                return true;
            } else if (itemId == R.id.nav_advertisement) {
                // 先设置选中项为广告，然后再跳转
                bottomNavigationView.setSelectedItemId(R.id.nav_advertisement);
                startActivity(new Intent(this, AdvertisementListActivity.class));
                finish();
                return true;
            } else if (itemId == R.id.nav_project) {
                // 先设置选中项为项目，然后再跳转
                bottomNavigationView.setSelectedItemId(R.id.nav_project);
                startActivity(new Intent(this, ProjectActivity.class));
                finish();
                return true;
            } else if (itemId == R.id.nav_promotion) {
                // 先设置选中项为推广，然后再跳转
                bottomNavigationView.setSelectedItemId(R.id.nav_promotion);
                startActivity(new Intent(this, PromotionActivity.class));
                finish();
                return true;
            } else if (itemId == R.id.nav_my) {
                // 当前页面，无需操作
                return true;
            }
            return false;
        });
    }

    private void loadUserData() {
        // 获取用户数据
        UserInfo userInfo = MockDataManager.findUserByUsername(currentUsername);
        if (userInfo != null) {
            tvUsername.setText(userInfo.getUsername());
            tvLevel.setText("Lv." + userInfo.getLevel());
            tvBalance.setText(userInfo.getBalance());
            
            // 设置收益数据
            MockDataManager mockDataManager = MockDataManager.getInstance();
            tvTodayIncome.setText(String.format("%.2f", mockDataManager.getTodayIncome(currentUsername)));
            tvWeekIncome.setText(String.format("%.2f", mockDataManager.getUserTodayIncome(currentUsername)));
            tvMonthIncome.setText(String.format("%.2f", mockDataManager.getUserTodayIncome(currentUsername)));
        }

        // 加载收益记录
        loadIncomeRecords(currentUsername);
    }

    private void loadIncomeRecords(String username) {
        MockDataManager mockDataManager = MockDataManager.getInstance();
        List<IncomeRecord> records = mockDataManager.getUserIncomeRecords(username);
        incomeRecordAdapter.updateData(records);
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadUserData();
        loadIncomeRecords(currentUsername);
    }
} 